﻿using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Presentation.Commands;
using TelecoSystems.Infrastructure.Helpers;
using TelecoSystems.Web.Models;
using TelecoSystems.Web.Services.Products;

namespace TelecoSystems.Modules.Products.ViewModels {
    public class EditVariantViewModel : INotifyPropertyChanged {
        #region Private Fields
        private ProductsDomainContext _productsCtx;
        #endregion

        #region Constructors and Factory methods
        public EditVariantViewModel(ProductsDomainContext productsCtx, ProductsBrowseViewModel productsModel, ChildWindow parentWindow) {
            _productsCtx = productsCtx;
            _variantDetail = productsModel.VariantDetail;
            _targetGroups = productsModel.TargetGroups;

            SubmitCommand = new DelegateCommand<object>(
                o => {
                    productsModel.SubmitChangesCommand.Execute(new object());
                    parentWindow.Close();
                },
                o => {
                    if (!VariantDetail.HasValidationErrors) {
                        return (VariantDetail.EntityState == EntityState.New) || (VariantDetail.HasChanges);
                    } else
                        return false;
                });

            CancelCommand = new DelegateCommand<object>(
                o => {
                    if (VariantDetail.HasChanges) {
                        ((IRevertibleChangeTracking)VariantDetail).RejectChanges();
                    }
                    parentWindow.Close();
                },
                o => {
                    return true;
                });

            _variantDetail.PropertyChanged += (s, e) => {
                if (SubmitCommand != null)
                    SubmitCommand.RaiseCanExecuteChanged();
            };
        }
        #endregion

        #region VM Properties
        private Variant _variantDetail;
        public Variant VariantDetail {
            get {
                return _variantDetail;
            }
            set {
                if (_variantDetail != value) {
                    _variantDetail = value;
                    NotifyPropertyChanged("VariantDetail");
                }
            }
        }

        private EntityCollectionView<TargetGroup> _targetGroups;
        public EntityCollectionView<TargetGroup> TargetGroups {
            get {
                return _targetGroups;
            }
            set {
                if (_targetGroups != value) {
                    _targetGroups = value;
                    NotifyPropertyChanged("TargetGroups");
                }
            }
        }
        #endregion

        #region Commands
        public DelegateCommand<object> SubmitCommand { get; set; }
        private void Submit(object o) {
            _productsCtx.SubmitChanges();
        }
        private bool CanSubmit(object o) {
            return VariantDetail.HasChanges;
        }

        public DelegateCommand<object> CancelCommand { get; set; }
        private void Cancel(object o) {
            _productsCtx.SubmitChanges();
        }
        private bool CanCancel(object o) {
            return VariantDetail.HasChanges;
        }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
